﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using GSdk.Net.Lglcd;
using System.Windows;
using System.Windows.Markup;
using System.Xaml;
using GSdk.Shared.Windows.Properties;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace GSdk.Shared.Windows
{
    public abstract class Window : ContentControl, IDrawable
    {
        private Action m_DispatcherDrawHandler;
        private Thread m_DrawThread;
        private Bitmap Background = QvgaDevice.CreateValidBitmap();

        public Window(Applet applet = null)
        {
            m_DispatcherDrawHandler = new Action(DispatcherDrawHandler);
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                Applet = new Applet()
                {
                    Autostartable = false,
                    SupportedDevices = DeviceType,
                    Name = GetType().FullName,
                };
                Applet.Connect();
                Applet.GetDevices<DefaultDeviceCollection>().Qvga.ForegroundApplet = true;
                Applet.GetDevices<DefaultDeviceCollection>().Qvga.AppletPriority = AppletPriorities.Alert;

                m_DrawThread = new Thread(new ThreadStart(DrawHandler));
                m_DrawThread.Start();
                //Dispatcher.Hooks.DispatcherInactive += new EventHandler(Hooks_DispatcherInactive);
            }
            else
            {
                Applet = applet ?? CreateApplet();
                //if (Applet == null) throw new NullReferenceException("CreateApplet() must return a not null value");
            }
        }

        private void DispatcherDrawHandler()
        {
            Applet.GetDevices<DefaultDeviceCollection>().Qvga.Update(Draw());
        }

        private void DrawHandler()
        {
            while (!Disposing)
            {
                Dispatcher.Invoke(m_DispatcherDrawHandler);
                Thread.Sleep(33);
            }
        }

        /*private void Hooks_DispatcherInactive(object sender, EventArgs e)
        {
            Dispatcher.Invoke(new Action(() =>
            {
            }));
        }*/

        protected abstract Applet CreateApplet();

        //public readonly object SyncRoot = new object();
        public Applet Applet { get; private set; }
        public override DeviceTypes DeviceType { get { return DeviceTypes.Qvga; } }

        public virtual Bitmap Draw()
        {
            using (Graphics g = Graphics.FromImage(Background))
            {
                g.FillRectangle(Brushes.Red, new Rectangle(0, 0, Background.Width, Background.Height));
                g.DrawString("Hello World", Font, Brushes.White, new PointF(0f, 0f));

                IDrawable ctrl = Content as IDrawable;
                if (ctrl != null)
                    g.DrawImage(ctrl.Draw(), 0, 0);
            }
            return Background;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            lock (Applet)
                if (Applet != null) Applet.Dispose();
        }
    }
}
